Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Aug 27, 2025

Summary

This PR adds support for configuring the Condensing Trigger Threshold in tokens (in addition to the existing percentage-based configuration).

Changes

  • Modified the sliding window logic to support token-based thresholds
  • Values above 100 are now treated as token counts rather than percentages
  • Token thresholds take precedence when both percentage and token thresholds could apply
  • Added comprehensive tests for the new token-based threshold functionality

How it works

  • Percentage thresholds: Values between 5-100 continue to work as percentages of the context window
  • Token thresholds: Values above 100 are treated as absolute token counts
  • Special value -1: Continues to mean "inherit from global setting"

Example Usage

Users can now configure profile-specific thresholds like:

  • 50 = Condense when context reaches 50% of the window
  • 8000 = Condense when context reaches 8000 tokens (regardless of window size)

This provides more precise control for profiles with known models and token limitations.

Testing

  • Added 3 new test cases for token-based thresholds
  • All existing tests pass without modification
  • Tests verify that token thresholds trigger correctly and take precedence over percentage thresholds

Fixes #7440


Important

Adds token-based condensing thresholds to truncateConversationIfNeeded() in index.ts, with tests in sliding-window.spec.ts.

  • Behavior:
    • Adds token-based condensing thresholds in truncateConversationIfNeeded() in index.ts.
    • Values >100 are treated as token counts, taking precedence over percentage thresholds.
    • Special value -1 continues to mean "inherit from global setting".
  • Testing:
    • Adds tests for token-based thresholds in sliding-window.spec.ts.
    • Tests ensure token thresholds trigger correctly and take precedence over percentage thresholds.
  • Misc:
    • Updates logic to handle both percentage and token thresholds, with token thresholds taking precedence.

This description was created by Ellipsis for be1511a. You can customize this summary. It will automatically update as commits are pushed.

- Allow configuring condensing threshold in tokens (values > 100)
- Percentage thresholds remain for values between 5-100
- Token thresholds take precedence when both could apply
- Add comprehensive tests for token-based thresholds

Addresses #7440
@roomote roomote bot requested review from cte, jr and mrubens as code owners August 27, 2025 01:13
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Aug 27, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 27, 2025
Copy link
Contributor Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing my own code is like debugging in a mirror - everything looks backwards but the bugs are still mine.

// Truncate if we're within TOKEN_BUFFER_PERCENTAGE of the context window
const allowedTokens = contextWindow * (1 - TOKEN_BUFFER_PERCENTAGE) - reservedTokens

// Determine the effective threshold to use
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be helpful to add a comment here explaining the threshold logic? Something like:

Suggested change
// Determine the effective threshold to use
// Determine the effective threshold to use
// Values interpretation:
// -1: inherit from global setting
// 5-100: percentage of context window
// >100: absolute token count
let effectiveThreshold = autoCondenseContextPercent

This would make it immediately clear to future maintainers how the different threshold values are interpreted.

// Valid percentage threshold
effectiveThreshold = profileThreshold
} else if (profileThreshold > MAX_CONDENSE_THRESHOLD) {
// Values above 100 are treated as token counts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider adding an upper bound check for token thresholds? For example, values larger than typical context windows (e.g., > 1,000,000) might indicate a configuration error.

effectiveTokenThreshold = profileThreshold
} else {
// Invalid threshold value, fall back to global setting
console.warn(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning message could be more informative about the new token-based thresholds:

Suggested change
console.warn(
console.warn(
`Invalid profile threshold ${profileThreshold} for profile "${currentProfileId}". Valid values are: -1 (inherit), 5-100 (percentage), or >100 (token count). Using global default of ${autoCondenseContextPercent}%`,
)

const role = i % 2 === 0 ? "user" : "assistant"
// Create content that roughly corresponds to the desired token count
// This is a simplification - actual token count depends on the tokenizer
const content = "x".repeat(tokensPerMessage * 4) // Rough approximation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth adding a comment explaining this approximation? The 4 characters per token ratio is a testing simplification that future maintainers might find helpful to understand.

} else if (profileThreshold >= MIN_CONDENSE_THRESHOLD && profileThreshold <= MAX_CONDENSE_THRESHOLD) {
// Valid custom threshold
// Valid percentage threshold
effectiveThreshold = profileThreshold
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extracting the magic number 100 as a constant like PERCENTAGE_TOKEN_BOUNDARY. This would make the distinction between percentage and token thresholds clearer throughout the code.

@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Aug 28, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Aug 28, 2025
@daniel-lxs
Copy link
Member

Closing this PR since it doesn't properly implement a token-based context threshold. The UI element to change the value for the percentage is a slider that only goes up to 100%. To properly implement this feature, the user should be able to switch between percentage and token modes explicitly, rather than using the ambiguous heuristic of treating values >100 as token counts. This approach would require UI changes to add a toggle or dropdown for selecting the threshold type.

@daniel-lxs daniel-lxs closed this Sep 1, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Sep 1, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Prelim Review] to Done in Roo Code Roadmap Sep 1, 2025
@daniel-lxs daniel-lxs deleted the feature/token-based-condensing-threshold branch September 1, 2025 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request PR - Needs Preliminary Review size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Configure Condensing Trigger Threshold in tokens

4 participants